package com.maoye.ossp.biz.intent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.contract.entity.AdvertAssociated;
import com.maoye.ossp.biz.contract.service.IAdvertAssociatedService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAdvertdiscard;
import com.maoye.ossp.biz.intent.service.IIntAdvertdiscardService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:联营作废意向 控制器类 
 * 开发公司:MOI 
 * 开发人员:zhongjt 
 * 创建时间:2014-02-20 14:50:38
 */

@Controller
@RequestMapping("biz/intent/intAdvertdiscard")
public class IntAdvertdiscardController extends BaseController {
	@Resource
	private IIntAdvertdiscardService intAdvertdiscardService;
	@Resource
	private IAdvertAssociatedService  contractServce;
	/**
	 * 列表展示意向信息
	 * @param intAdvertdiscard
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月11日
	 */
	@RequestMapping(value = "list")
	public String list(IntAdvertdiscard intAdvertdiscard, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// TODO 等待宏天分页功能完善   Page page = new Page();
		QueryFilter queryFilter = getQuerFilter(request,intAdvertdiscard);
		queryFilter.addFilter("area", intAdvertdiscard.getArea(), QueryOP.EQUAL);
		queryFilter.addFilter("areaId", intAdvertdiscard.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floor", intAdvertdiscard.getFloor(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intAdvertdiscard.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("intentNo", intAdvertdiscard.getIntentNo(), QueryOP.EQUAL);
		queryFilter.addFilter("store", intAdvertdiscard.getStore(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intAdvertdiscard.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierNo", intAdvertdiscard.getSupplierNo(), QueryOP.EQUAL);
		queryFilter.addFilter("status", intAdvertdiscard.getStatus(), QueryOP.EQUAL);
		queryFilter.addFilter("createTime", intAdvertdiscard.getCreateTime(), QueryOP.EQUAL);
		String createBy = null;
		if(intAdvertdiscard.getCreateBy() !=null){
			createBy = ContractUtils.getUserId(intAdvertdiscard.getCreateBy());
		}
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		Page<IntAdvertdiscard> page = intAdvertdiscardService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/intent/intAdvertdiscard/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntAdvertdiscard intAdvertdiscard, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAdvertdiscardService.save(intAdvertdiscard);
		
		addMessage(redirectAttributes, "保存'" + intAdvertdiscard.getSupplierName()
				+ "合同作废意向'成功");
		return "redirect:/biz/intent/intAdvertdiscard/list?navId="+navId;
	}
	
	
	
	@RequestMapping(value = "submit")
	public String submit(IntAdvertdiscard intAdvertdiscard, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAdvertdiscardService.submit(intAdvertdiscard);
		
		addMessage(redirectAttributes, "保存'" + intAdvertdiscard.getSupplierName()
				+ "合同作废意向'成功");
		return "redirect:/biz/intent/intAdvertdiscard/list?navId="+navId;
	}
	
	
	@RequestMapping(value = "{negId}/add")
	public String add( @PathVariable Long negId,Model model,IntAdvertdiscard intAdvertdiscard , HttpServletRequest request) {
		
		model=intAdvertdiscardService.init(negId,model);
		
		return resultPage("/biz/intent/intAdvertdiscard/add", model, request);
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model, HttpServletRequest request) {
		IntAdvertdiscard intAdvertdiscard = intAdvertdiscardService.get(id);
		AdvertAssociated  associatedContract  = contractServce.get(intAdvertdiscard.getContractId().toString());
		model.addAttribute("intAssdiscard", intAdvertdiscard);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intAdvertdiscard/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id,HttpServletRequest request, Model model) {
		IntAdvertdiscard intAdvertdiscard = intAdvertdiscardService.get(id);
		AdvertAssociated  associatedContract  = contractServce.get(intAdvertdiscard.getContractId().toString());
		model.addAttribute("intAdvertdiscard", intAdvertdiscard);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intAdvertdiscard/view", model, request);
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		intAdvertdiscardService.remove(id);
		addMessage(redirectAttributes, "删除合同作废意向成功！");
		return "redirect:/biz/intent/intAdvertdiscard/list";
	}
	/**
	 * 修改合同状态
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yangh
	 * @data 2014年4月10日
	 */
	@RequestMapping(value = "updateContractType")
	public String updateContractType( Model model,
			RedirectAttributes redirectAttributes) {
		return "redirect:/biz/intent/intAdvertdiscard/list";
	}
	/**
	 * 批量刪除
	 * @param ids
	 * @param request
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yangh
	 * @data 2014年5月21日
	 */
	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String item = intAdvertdiscardService.removeBatch(ids);
		if(!"".equals(item)){
			addMessage(redirectAttributes,"意向单号为"+item+"的作废意向已经进入流程审批，不能被删除！");
		}else{
			addMessage(redirectAttributes,"删除作废意向成功");
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/intent/intAdvertdiscard/list?navId="+navId;
	}
	
	/**
	 * 获取合同信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(AdvertAssociated associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) {
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		Page<AdvertAssociated> page = contractServce.find(queryFilter);
		int totalSize = (page.getTotal() + page.getPageSize() - 1) / page.getPageSize();
		model.addAttribute("page", page);
		model.addAttribute("totalSize", totalSize);
		return resultPage("/biz/intent/common/modal", model, request);
	}
	/**
	 * 获取合同信息(分页)
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @throws IOException 
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractListPage")
	public void getModuleContractListPage(AdvertAssociated associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) throws IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		String supplierName =associatedContract.getSupplierName();
		String contractNo = associatedContract.getContractNo();
		String subject = associatedContract.getSubject();
		queryFilter.addFilter("supplierName", supplierName!=""?supplierName:null, QueryOP.LIKE);
		queryFilter.addFilter("contractNo", contractNo !=""?contractNo:null, QueryOP.LIKE);
		queryFilter.addFilter("subject", subject!=""?subject:null, QueryOP.LIKE);
		Page<AdvertAssociated> page = (Page<AdvertAssociated>) contractServce.find(queryFilter);
		List<AdvertAssociated> list = page.getList();
		
		StringBuffer sb = new StringBuffer(); 
		sb.append("{\"total\":\""+page.getTotal()+"\",");
		sb.append("\"pageNo\":\""+page.getPageNo()+"\",");
		sb.append("\"pageSize\":\""+page.getPageSize()+"\",");
		sb.append("\"jsonRoot\":[");
		
		for(int i =0;i<list.size();i++){
			AdvertAssociated  contract = list.get(i);
			sb.append("{\"id\":\""+ contract.getId());  
            sb.append("\",");
            sb.append("\"supplierName\":\""+contract.getSupplierName()+"\",");
            sb.append("\"subject\":\""+contract.getSubject()+"\",");
            sb.append("\"contractNo\":\""+contract.getContractNo()+"\"");
			sb.append("},");
		}
		if(list.size()>0){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		sb.append("]}"); 
        out.print(sb);
        out.flush();
		out.close();
	}
}
